﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace YouDaoNotesCodes
{
    class ConvertXmlJson
    {
        #region xml转json
        public void XmlTOJson()
        {
            XmlDocument doc = new XmlDocument();
            string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                                        + " <users>"
                                        + " <user id = '1'>"
                                        + " <name>Tom</name>"
                                        + " <password>aaa</password>"
                                        + " </user>"
                                        + " </users>";
            doc.LoadXml(xml);

            string jsonText = JsonConvert.SerializeXmlNode(doc);//xml转json

            Console.WriteLine(jsonText);
            Console.ReadKey();
        }
        #endregion

        #region json转xml
        public void JsonToXml()
        {
            string json = @"{
                              ""?xml"": {
                                ""@version"": ""1.0"",
                                ""@standalone"": ""no""
                              },
                              ""root"": {
                                ""person"": [
                                  {
                                    ""@id"": ""1"",
                                    ""name"": ""Alan"",
                                    ""url"": ""http://www.google.com""
                                  },
                                  {
                                    ""@id"": ""2"",
                                    ""name"": ""Louis"",
                                    ""url"": ""http://www.yahoo.com""
                                  }
                                ]
                              }
                            }";
            XmlDocument doc = JsonConvert.DeserializeXmlNode(json);////json转xml

            Console.WriteLine(doc.OuterXml);
            Console.ReadKey();

        }
        #endregion
    }
}
